//
//  MXHomeListCell.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/11.
//

#import "MXHomeListCell.h"

@implementation MXHomeListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    self.selectionStyle = 0;
    self.backgroundColor = [UIColor clearColor];
    self.bgView = [[UIView alloc] init];
    self.bgView.layer.cornerRadius = 6;
    self.bgView.layer.masksToBounds = YES;
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.height.mas_equalTo(277);
        make.top.offset(11);
    }];
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.text = @"智能喂食器";
    self.titleLab.font = PFRFontMedium(16);
    self.titleLab.textColor = [UIColor colorWithHexString:@"#000000"];
    [self.bgView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(11);
        make.width.mas_lessThanOrEqualTo(kScreenW - 150);
//        make.height.mas_equalTo(14);
    }];
    
    self.tagLab = [[UILabel alloc] init];
    [self.bgView addSubview:self.tagLab];
    self.tagLab.textAlignment = NSTextAlignmentCenter;
    self.tagLab.text = @"可视版";
    self.tagLab.textColor = [UIColor dc_colorWithHexString:@"#000000"];
    self.tagLab.font = PFRFont(8);
    self.tagLab.layer.cornerRadius = 4;
    self.tagLab.layer.masksToBounds = YES;
    self.tagLab.layer.borderWidth = 1;
    self.tagLab.layer.borderColor = [UIColor dc_colorWithHexString:@"#000000"].CGColor;
    [self.tagLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLab.mas_right).offset(3);
        make.centerY.mas_equalTo(self.titleLab);
        make.height.mas_equalTo(12);
        make.width.mas_equalTo(39);
    }];
    
    self.stateBtn = [UIButton buttonWithType:0];
    [self.stateBtn setTitle:@"离线" forState:0];
    [self.stateBtn setTitleColor:[UIColor dc_colorWithHexString:@"#000000"] forState:0];
    self.stateBtn.titleLabel.font = PFRFont(14);
//    [self.stateBtn setImage:[UIImage imageNamed:@"home_outline"] forState:0];
    [self.bgView addSubview:self.stateBtn];
    [self.stateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(46);
        make.height.mas_equalTo(12);
        make.centerY.mas_equalTo(self.titleLab);
        make.right.offset(-13);
    }];
    [self.stateBtn dc_buttonIconLeftWithSpacing:5];
    
    self.memberTipLab = [[UILabel alloc] init];
    [self.bgView addSubview:self.memberTipLab];
    self.memberTipLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.5];
    self.memberTipLab.font = PFRFont(12);
    self.memberTipLab.text = @"家庭成员";
    [self.memberTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(13);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(5);
//        make.height.mas_equalTo(10);
    }];
    
    self.memberLab = [[UILabel alloc] init];
    [self.bgView addSubview:self.memberLab];
    self.memberLab.textColor = [UIColor dc_colorWithHexString:@"#000000"];
    self.memberLab.font = PFRFont(12);
    self.memberLab.text = @"1";
    [self.memberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.memberTipLab.mas_right).offset(5);
        make.centerY.mas_equalTo(self.memberTipLab);
    }];
    
    self.videoBg = [[UIView alloc] init];
    self.videoBg.layer.cornerRadius = 6;
    self.videoBg.layer.masksToBounds = YES;
    self.videoBg.userInteractionEnabled = YES;
    self.videoBg.backgroundColor = [UIColor colorWithHexString:@"#D1D1D1"];
    [self.bgView addSubview:self.videoBg];
    [self.videoBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
//        make.top.offset(46);
        make.top.mas_equalTo(self.memberTipLab.mas_bottom).offset(5);
    }];
    
    self.stateV = [[UIView alloc] init];
    [self.videoBg addSubview:self.stateV];
    self.stateV.backgroundColor = [UIColor colorWithHexString:@"#009944"];
    self.stateV.layer.cornerRadius = 3;
    self.stateV.layer.masksToBounds = YES;
    [self.stateV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(6);
        make.left.offset(15);
        make.bottom.offset(-11);
    }];
    
    self.stateLab = [[UILabel alloc] init];
    self.stateLab.text = @"实时";
    self.stateLab.textColor = [UIColor whiteColor];
    self.stateLab.font = PFRFont(12);
    [self.videoBg addSubview:self.stateLab];
    [self.stateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.centerY.mas_equalTo(self.stateV);
    }];
    
    self.linkImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_link"]];
    [self.videoBg addSubview:self.linkImageV];
    [self.linkImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.videoBg);
        make.width.mas_equalTo(26);
        make.height.mas_equalTo(20);
    }];
    
    MJWeakSelf;
    self.setFoodV = [[MXHomeSetFoodView alloc] init];
    [self.bgView addSubview:self.setFoodV];
    [self.setFoodV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.videoBg.mas_bottom).offset(10);
        make.left.offset(11);
        make.bottom.offset(-12);
        make.height.mas_equalTo(30);
        make.right.offset(-100);
    }];
}

- (void)reloadCellWithModel:(MXHomeListModel *)model {
    
    self.titleLab.text = model.deviceLocalName;
    [self.setFoodV reloadCount:model.feedCopiesNum];
    
    [self.stateBtn setTitle:(model.deviceStatus == 0 ? @"离线" : @"在线") forState:0];
    [self.stateBtn setTitleColor:[UIColor dc_colorWithHexString:(model.deviceStatus == 0 ? DC_333333 : @"#009944")] forState:0];
    
    self.memberLab.text = [NSString stringWithFormat:@"%lu",(unsigned long)model.deviceForFamilyMemberCount];
}

@end
